package com.cz.basetool.ui_work.util.tts;

import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;

import java.util.HashMap;
import java.util.Locale;

public class TTSController implements OnInitListener {

	private static TTSController mManager;

	private TextToSpeech textToSpeech;

	private boolean isInit = false;

	private static final int SPEAK_ADD = 0;

	private static final int SPEAK_FLUSH = 1;

	private static final String TAG = "TTSControler";

	private HashMap<String, String> myHashAlarm = new HashMap<String, String>();
	private long lastSpeekTime;
	private Handler speakHandler = new Handler() {
		@SuppressWarnings("deprecation")
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SPEAK_ADD:
				if (textToSpeech != null) {
					if (isInit) {
						textToSpeech.speak(msg.obj.toString(),
								TextToSpeech.QUEUE_ADD, myHashAlarm);
					} else {
						speakHandler.sendMessageDelayed(
								speakHandler.obtainMessage(SPEAK_ADD, msg.obj),
								1000);
					}
				}
				break;

			case SPEAK_FLUSH:
				if (textToSpeech != null) {
					if (isInit) {
						textToSpeech.speak(msg.obj.toString(),
								TextToSpeech.QUEUE_FLUSH, myHashAlarm);
					} else {
						speakHandler.sendMessageDelayed(speakHandler
								.obtainMessage(SPEAK_FLUSH, msg.obj), 1000);
					}
				}

				break;

			default:
				break;
			}
		};
	};

	public static TTSController getInstance() {
		if (mManager == null) {
			mManager = new TTSController();
		}
		return mManager;
	}

	public void init(Context context) {
		if (textToSpeech == null) {
			textToSpeech = new TextToSpeech(
					context, this);

		}
		if (myHashAlarm.size() < 1) {
			myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
					String.valueOf(AudioManager.STREAM_MUSIC));
		}
	}

	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = textToSpeech.setLanguage(Locale.CHINESE);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
			}
			isInit = true;
			AndroidConsoleLogPrinter.e(TAG, "TTS初始化成功");
		} else {
			AndroidConsoleLogPrinter.e(TAG, "TTS初始化失败");
			isInit = false;
		}
	}

	public boolean isInit() {
		return this.isInit;
	}

	/**
	 * 如果语音队列中有任务，就添加到后面，不打断，一直说完
	 * @param message
	 */
	public void speakAdd(String message) {
		lastSpeekTime = System.currentTimeMillis();
		speakHandler
				.sendMessage(speakHandler.obtainMessage(SPEAK_ADD, message));
		AndroidConsoleLogPrinter.e(TAG, "speakAdd: " + message);
	}


	public void speakAddInTime(String message,long separateTime) {
		if(System.currentTimeMillis() - lastSpeekTime>separateTime){
			speakRightNow(message);
		}
	}
	/**
	 * 立马说，清除掉上一次的语音
	 * 
	 * @param message
	 */
	public void speakRightNow(String message) {
		lastSpeekTime = System.currentTimeMillis();
		speakHandler.sendMessage(speakHandler.obtainMessage(SPEAK_FLUSH,
				message));
	}

	public void stop() {
		if (textToSpeech != null)
			textToSpeech.stop();
	}

	public void realse() {
		if (textToSpeech != null) {
			textToSpeech.stop();
			textToSpeech.shutdown();
		}
		if (mManager != null) {
			mManager = null;
		}

	}

}